<?php
require_once "config.php";
require_once "Auth/Auth.php";
require_once "Auth/Controller.php";

class AuthSingleton extends Auth {
    
    static $configSection = 'AUTH';
    static $instance = array(); 

    static function redirect($username = null, $status = null, &$auth = null)
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI',AuthSingleton::$configSection);
        
        
        $loginPage = $options['login_page'] ;
            
        if ( !strpos($options['login_page'],"?") ) 
        {
            $loginPage .= "?";
        }
        $loginPage .= "&status=".$auth->getStatus()."&username=".$username;
        trigger_error("ENTER AuthSingleton::redirect() QUERY_STRING='".$_SERVER['QUERY_STRING']."'", E_USER_NOTICE);
        $location = strlen($_SERVER['QUERY_STRING']) && !strstr($_SERVER['QUERY_STRING'],'logout') 
                    ?  $_SERVER['QUERY_STRING'] : "cmd=".$options['auth_default_page'] ;
        setcookie('location',$location,0 );
        trigger_error("AuthSingleton::redirect() location=".$location, E_USER_NOTICE);
        session_write_close();
        header("Location: ".$loginPage);
        exit();
    }


    static function loginCallback($username = null,  &$auth = null )
    {
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $location = $_COOKIE['location'];
        setcookie('location','',time()-3600);
        trigger_error("ENTER AuthSingleton::loginCallback() location=$location", E_USER_NOTICE);
        session_write_close();
        header("Location: ".rtrim($global['service_url'],'/')."/?".$location);
        exit();
    }

    static function logoutCallback($username = null,  &$auth = null )
    {
        trigger_error("ENTER AuthSingleton::logoutCallback()", E_USER_NOTICE);
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $options = &PEAR::getStaticProperty('CONFIG_INI',AuthSingleton::$configSection);
        session_unset();
        header("Location: ".rtrim($global['service_url'],'/')."/?cmd=".$options["auth_default_page"]); 
        exit();
    }



    function &instance($configSection = 'AUTH' ) 
    {

        AuthSingleton::$configSection = $configSection;

        if(!isset(AuthSingleton::$instance[AuthSingleton::$configSection])) { 

            $dsn = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

            $authOptions = &PEAR::getStaticProperty('CONFIG_INI',AuthSingleton::$configSection);
            
            $options = array(
                'enableLogging' => true,
                'sessionName' => $authOptions['session_name'],
                'dsn' => $authOptions['dsn_string'],
                'table' => $authOptions['table'],
                'usernamecol' => $authOptions['usernamecol'],
                'passwordcol' => $authOptions['passwordcol'],
                'auto_quote' => $authOptions['auto_quote'],
                'db_options' => AuthSingleton::getDbOptionsArray( $authOptions['db_options'] )    
            );

            if( !file_exists( $authOptions['auth_container_file'] ) || !is_file( $authOptions['auth_container_file'] )  )
            {
                trigger_error("Incorrect Auth container file ".$authOptions['auth_container_file'], E_USER_ERROR);
            }
            require_once $authOptions['auth_container_file'];
            
            if ( !class_exists( $authOptions['auth_container_class'] ) )
            {
                trigger_error("Auth container class ".$authOptions['auth_container_class']." was not defined.", E_USER_ERROR);
            }
            
            $_auth_container = new $authOptions['auth_container_class']($options);

            AuthSingleton::$instance[AuthSingleton::$configSection] = new Auth($_auth_container, $options,'AuthSingleton::redirect');
            AuthSingleton::$instance[AuthSingleton::$configSection]->setLoginCallback('AuthSingleton::loginCallback');
            AuthSingleton::$instance[AuthSingleton::$configSection]->setLogoutCallback('AuthSingleton::logoutCallback');
            AuthSingleton::$instance[AuthSingleton::$configSection]->setExpire(0);
            AuthSingleton::$instance[AuthSingleton::$configSection]->setIdle(0);
        }

        return AuthSingleton::$instance[AuthSingleton::$configSection];
   }

    private static function getDbOptionsArray ( $string = "" )
    {

        $retArray = array();
        if ( ! strlen ( $string )  ) 
        {
            return $retArray;
        }
        
        $tmpArray = explode ( "," , $string );
        
        if ( !( $tmpArray || count($tmpArray) ) )
        {
            return $retArray; 
        }
        
        foreach ( $tmpArray as $value ) 
        {
            $tmpParamValue = explode ("|", $value );
            if ( !( $tmpParamValue || count($tmpParamValue)  ) ) 
            {
                continue;
            }
    
            $par = trim( $tmpParamValue[0]  );
            $val = trim( $tmpParamValue[1] );
            
            $retArray[$par] = $val;
        }
        
        return $retArray;
    }
}

?>
